#!/bin/bash
#source a_function_result.sh



JQ_EXEC=`which jq`

#getChannelValueFromFile -pagyerDownJsonF "${Pgyer_Download_FILE_PATH}" -envJsonF "${ENVIRONMENT_FILE_PATH}"
function getChannelValueFromFile() {
    while [ -n "$1" ]
    do
            case "$1" in
                    -pagyerDownJsonF|--pgyer-download-json-file) Pgyer_Download_FILE_PATH=$2; shift 2;;
                    -envJsonF|-environment-json-file) ENVIRONMENT_FILE_PATH=$2; shift 2;;
                    --) break ;;
                    *) echo $1,$2,$show_usage; break ;;
            esac
    done
    
    echo "正在执行方法：$FUNCNAME"
    
    targetBranchConfig_String=$(cat ${Pgyer_Download_FILE_PATH} | ${JQ_EXEC} -r ".package_result.pgyer_branch_config")
    if [ -z "${targetBranchConfig_String}" ] || [ "${targetBranchConfig_String}" == "null" ]; then
        echo "请先配置蒲公英分支参数,否则后面无法去获取本次打包要使用的蒲公英参数"
        return 1
    fi
    
    echo "正在执行命令《getChannelValueFromPlatformPgyerBranchConfig \"${targetBranchConfig_String}\"》"
    getChannelValueFromPlatformPgyerBranchConfig "${targetBranchConfig_String}"
}

# 从 PgyerBranchConfig 中获取 Platform 指定的 pgyerChannelShortcut\pgyerChannelKey等(为了将其设置到.package_result.pgyer_branch_config)
function getChannelValueFromPgyerBranchConfigWithPlatform() {
#    network_branch_platform_pgyerChannelShortcut=$(cat $FILE_PATH | ${JQ_EXEC} '.package_result.shoudUploadToPgyerChannelShortcut' | sed 's/\"//g')

    targetBranchConfig_String=$1
    platformKey=$2  #'iOS'

    network_branch_platform_pgyerRootMapString=$(echo "${targetBranchConfig_String}" | ${JQ_EXEC} -r --arg platformKey "$platformKey" '.[$platformKey]')
    echo "*************************network_branch_platform_pgyerRootMapString=${network_branch_platform_pgyerRootMapString}"
    if [ -z "${network_branch_platform_pgyerRootMapString}" ] || [ "${network_branch_platform_pgyerRootMapString}" == "null" ]; then
        return 1
    fi
    
    getChannelValueFromPlatformPgyerBranchConfig "${network_branch_platform_pgyerRootMapString}"
}

function getChannelValueFromPlatformPgyerBranchConfig() {
    network_branch_platform_pgyerRootMapString=$1
    
    # upload
    packagePgyerChannelShortcutResult_upload=$(echo "${network_branch_platform_pgyerRootMapString}" | ${JQ_EXEC} -r ".uploadChannelShortcut")
    packagePgyerChannelKeyResult_upload=$(echo "${network_branch_platform_pgyerRootMapString}" | ${JQ_EXEC} -r ".uploadChannelKey")
    echo "packagePgyerChannelShortcutResult_upload=${packagePgyerChannelShortcutResult_upload}"
    echo "packagePgyerChannelKeyResult_upload=${packagePgyerChannelKeyResult_upload}"
    # download
    packagePgyerChannelShortcutResult_download=$(echo "${network_branch_platform_pgyerRootMapString}" | ${JQ_EXEC} -r ".downloadChannelShortcut")
    packagePgyerChannelKeyResult_download=$(echo "${network_branch_platform_pgyerRootMapString}" | ${JQ_EXEC} -r ".downloadChannelKey")
    echo "packagePgyerChannelShortcutResult_download=${packagePgyerChannelShortcutResult_download}"
    echo "packagePgyerChannelKeyResult_download=${packagePgyerChannelKeyResult_download}"
}


function checkUploadChannelValueFromPgyerBranchConfigWithPlatform() {
    targetBranchConfig_String=$1
    platformKey=$2  #'iOS'
    
    getChannelValueFromPgyerBranchConfigWithPlatform "${targetBranchConfig_String}" "${platformKey}"
    if [ -z "${packagePgyerChannelShortcutResult_upload}" ] || [ "${packagePgyerChannelShortcutResult_upload}" == "null" ]; then
        return 1
    fi
    
    # 需事先在蒲公英上建立此渠道短链，否则会提示The channel shortcut URL is invalid
    echo "上传目标：只会上传到蒲公英的上的【指定渠道】:${packagePgyerChannelShortcutResult_upload}"
    
    return 0
}

:<<!
Pgyer_Download_FILE_PATH="../bulidScript/app_info.json"
ENVIRONMENT_FILE_PATH="../bulidScript/app_info.json"
getChannelValueFromFile -pagyerDownJsonF "${Pgyer_Download_FILE_PATH}" -envJsonF "${ENVIRONMENT_FILE_PATH}"
!
